package com.coderfans.serialize;

import com.alibaba.fastjson.JSON;
import com.coderfans.exception.SerializeException;

/**
 * Created by ${fanchunshuai} on 2016-9-22.
 *
 * @version: V1.0
 * @Desc:
 * @Copyright (c) 2016 58到家-版权所有
 */
public class JsonSerialize implements ISerialize<Object> {
    @Override
    public byte[] serialize(Object o) throws SerializeException {
        if(o == null){
            throw new SerializeException("clazz is null,can't be fastjsonSerialized.");
        }
        return JSON.toJSONString(o).getBytes();
    }

    @Override
    public Object disSerialize(Class<Object> clazz, byte[] objectByte) throws SerializeException {
        if(objectByte.toString().equals("") || objectByte == null){
            throw new SerializeException("objectStr is null,can't be jsonDesrialized ");
        }
        String data = new String(objectByte);

        data = data.replace("[","").replace("]","");
        //System.out.println("data = "+data);
        return JSON.parseObject(data,clazz);
    }
}
